module(...,package.seeall)

function avbv(avbv,func)
 getConfig(avbv,function(code,content)
    if code==200 then
      if string.lower(avbv):find"av"==1 then
        func(code,content.video.bvid)
       elseif string.lower(avbv):find"bv"==1 then
        func(code,"av"..tostring(int(content.video.aid)))
      end
     else
      func(code,content)
    end
  end)
  --return _ENV
end
function getCover(avbv,func)
  getConfig(avbv,function(code,content)
    if code==200 then
      func(code,content.video.viewInfo.pic)
     else
      func(code,content)
    end
  end)
  --return _ENV
end

function getConfig(avbv,func)
  if string.lower(avbv):find"av"==1 or string.lower(avbv):find"bv"==1 then
    Http.get("https://m.bilibili.com/video/"..avbv,nil,"UTF-8",nil,function(code,content,cookie,header)
      if code==200 then
        if content:find([[<title data%-vue%-meta%="true">啊叻？视频不见了？</title>]]) or content:find([[<meta name="title" content="什么都没找到" />]]) then
          func(404,"获取失败")
         else
          local content=content:match([[<script>window.__INITIAL_STATE__=(.+);%(function%(%){var s;%(s=document%.currentScript||document%.scripts%[document%.scripts%.length%-1%]%)%.parentNode%.removeChild%(s%);}%(%)%);</script>]])
          func(code,cjson.decode(content))
        end
       else
        func(code,"网络错误")
      end
    end)
   else
    func(404,"输入无效")
  end
  --return _ENV
end
